import config from './config.js'
import store from "@/store/index.js";
module.exports = (param) => {
	console.log('调用了接口：'+param.url);
	let url = param.url;
	let method = param.method;
	let header = param.header || {};
	console.log('有没有openid：'+store.state.user.openid);
	let openidObj = store.state.user.openid?{xc_open_id:store.state.user.openid||uni.getStorageSync(config.openid)}:{xc_open_id:(uni.getStorageSync(config.openid)||'')};
	console.log(openidObj);
	let data = Object.assign({}, param.data||{}, openidObj)
	
	// 请求方式: GET POST 
	if(method){
		method = method.toUpperCase(); // 小写转成大写
		if(method == "POST"){
			header = {"content-type":"application/x-www-form-urlencoded","applet_id":config.applet_id}
		}else{
			header = {"applet_id":config.applet_id}
		}
	}
	
	// 发起请求 加载动画
	// if(!param.hideLoading){
	// 	uni.showLoading({title:"加载中..."});
	// }
	
	// 发起网络请求
	uni.request({
		url: url,
		method:method || "GET",
		header:header,
		data: data,
		success: res => {
			// uni.hideLoading();
			if(res.statusCode && res.statusCode != 200){ // api错误
				uni.showModal({
					content:res.msg
				})
				return;
			}
			
			typeof param.success == "function" && param.success(res.data);
		},
		fail: (e) => {
			uni.hideLoading();
			console.log(47,e)
			// uni.showModal({
			// 	content: e.errMsg
			// })
			uni.showToast({
				title:"请求失败！！！",
				icon:"none"
			})
			typeof param.fail == "function" && param.fail(e.data);
		},
		complete: () => {
			// console.log("网络请求complete");
			// uni.hideLoading();
			typeof param.complete == "function" && param.complete(e.data);
			return;
		}
	})
}